local app = require "app"
local gamelib = require "club.threecard.gamelib"
local slotlib = require "club.threecard.slotlib"
local components = slotlib.require "components"

local slot_system = slotlib.systems.slot_system
local class = gamelib.entitas.class
local Matcher = gamelib.entitas.Matcher
local GroupEvent = gamelib.entitas.GroupEvent

local launch_machine_system = class("launch_machine_system", slot_system)

function launch_machine_system:initialize()
    local pay_tip = self.ingame.pay_tip;
    self.context.machine:replace(components.launch_machine_component, pay_tip);
end

function launch_machine_system:get_trigger()
    return {{Matcher({components.launch_machine_component}), GroupEvent.ADDED}};
end

function launch_machine_system:filter(entity)
    return entity:has(components.launch_machine_component)
end

function launch_machine_system:execute(entitas)
    if entitas:size() ~= 1 then
        gamelib.log.error("数据错误，结果组件不可能>1")
        return;
    end

    self.ingame.machine:start();
    entitas:foreach(function(e)
        local txt = e:get(components.launch_machine_component).tip_txt;
        if txt then
            txt.text = app.i18n.text("slot:good luck")
        end

        local c = e:get(components.result_info_component);
        while c.info == nil do
            self:step();
        end
    end)
end

return launch_machine_system